package com.baibei.product.adapter;

import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.product.R;

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by tang on 2018/1/10.
 */

public class CurrentAdapter extends RecyclerView.Adapter<CurrentAdapter.ViewHolder> implements View.OnClickListener {
    public ArrayList<Map<String,Object>> datas;
    public CurrentAdapter(ArrayList<Map<String,Object>> datas){
        this.datas = datas;
    }
    private OnItemClickListener onItemClickListener = null;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_current_ranking, viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onClick(View view) {
        if (onItemClickListener != null){
            onItemClickListener.onItemClick(view, (int) view.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.onItemClickListener = listener;
    }

    public static interface OnItemClickListener{
        void onItemClick(View view , int position);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.tvRank.setText(datas.get(i).get("rank").toString());
//        viewHolder.ivAvatar.setImageBitmap((Bitmap) datas.get(i).get("avatar"));
        viewHolder.tvUsername.setText(datas.get(i).get("username").toString());
        viewHolder.tvProfit.setText("累计盈利："+datas.get(i).get("profit").toString());
        viewHolder.itemView.setTag(i);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView tvRank;
//        private final ImageView ivAvatar;
        private final TextView tvUsername;
        private final TextView tvProfit;

        public ViewHolder(View itemView) {
            super(itemView);
            tvRank = itemView.findViewById(R.id.tv_rank);
//            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvProfit = itemView.findViewById(R.id.tv_profit);
        }
    }
}
